package com.zr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.model.User;
import com.zr.util.JDBCUtil;

/**
 * 创建用户登录使用的dao层
 * @author Administrator
 *
 */
public class UserDao {
	/**
	 * 查询到用户的用户名和密码
	 * @param user 传入用户
	 * @return 有则返回true
	 */
	public boolean validateUser(User user) {
		boolean  flag = false;
  	  String sql  = "select * from user where uname=? and upsw=?";
  	  Connection   con  =  JDBCUtil.getJDBConnection();
  	  try {
			PreparedStatement  pst = con.prepareStatement(sql);
			pst.setString(1, user.getUname());
			pst.setString(2, user.getUpsw());
			ResultSet  rs  = pst.executeQuery();
			if(rs.next()){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.closedConnection(con);
		}
  	  
  	  
  	  return flag;
	}
	/**
	 * 查询到一个用户的用户名和密码
	 * @param user 传入用户
	 * @return 返回查到的用户
	 */
		public  User getOneUser(User user){
				User currentUser  = new User();
	    	  String sql  = "select * from user  where uname=? and upsw=?";
	      	  Connection   con  =  JDBCUtil.getJDBConnection();
	      	  try {
	  			PreparedStatement  pst = con.prepareStatement(sql);
	  			pst.setString(1, user.getUname());
	  			pst.setString(2, user.getUpsw());
	  			ResultSet  rs  = pst.executeQuery();
	  			if(rs.next()){
	  				currentUser.setUid(rs.getInt("uid"));
	  				currentUser.setAid(rs.getInt("aid"));
	  				currentUser.setTid(rs.getInt("tid"));
	  				currentUser.setSid(rs.getInt("sid"));
	  				currentUser.setUname(rs.getString("uname"));
	  				currentUser.setUpsw(rs.getString("upsw"));
	  			}
	  		} catch (SQLException e) {
	  			// TODO Auto-generated catch block
	  			e.printStackTrace();
	  		}finally{
	  			JDBCUtil.closedConnection(con);
	  		}
	    	    return  currentUser;
	    }
		/**
		 * 检验当前用户用户名是否存在
		 * @param uname 传入当前用户的用户名
		 * @return 有则返回true
		 */
		public  boolean validataUname(String uname){
	    	   boolean  flag = false;
	    	   String sql  = "select uid from user where uname=?";
	    	   Connection   con  =  JDBCUtil.getJDBConnection();
	    	   try {
				PreparedStatement  pst = con.prepareStatement(sql);
				pst.setString(1,uname);
				ResultSet  rs = pst.executeQuery();
				if(rs.next()){
					flag = true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	   
	    	   return flag;
	       }
		/**
		 * 修改密码的方法
		 * @param newpsw 传入新密码
		 * @param userId 当前用户的id
		 * @return
		 */
		public  boolean  updatePsw(String newpsw,int userId){
	    	   boolean  flag = false;
	    	   String sql  = "update user set upsw=? where uid=?";
	    	   Connection   con  =  JDBCUtil.getJDBConnection();
	    	   try {
				PreparedStatement  pst = con.prepareStatement(sql);
				pst.setString(1, newpsw);
	  			pst.setInt(2, userId);
	  			int i=pst.executeUpdate();
	  			if(i>0){
	  				flag = true;
	  			}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				JDBCUtil.closedConnection(con);
			}
	    	   return  flag;
	}
}
